﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    
    public class MenuBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public List<MENU_TRAI> LayTatCa()
        {
            return DB.MENU_TRAIs.ToList();
        }
        public List<MENU_TRAI> LayDSMenuCapMot()
        {
            return DB.MENU_TRAIs.Where(m => m.CapHai == 0).OrderBy(m => m.CapMot).ToList();
        }
        public List<MENU_TRAI> LayDSMenuCapHai(int _CapMot)
        {
            return DB.MENU_TRAIs.Where(m => m.CapHai > 0 && m.CapMot == _CapMot).OrderBy(m => m.CapHai).ToList();
        }
        public List<MENU_TRAI> LayDSMenuCapHaiTheoRole(int _CapMot, int _MaNV)
        {
            PhanQuyenBLL pq = new PhanQuyenBLL();
            QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
            var lstUserRole = q.LayMaRoleTuNV(_MaNV);
            List<MENU_TRAI> mt = DB.MENU_TRAIs.Where(m => m.CapHai > 0 && m.CapMot == _CapMot && m.HienThi == 1).OrderBy(m => m.CapHai).ToList();
            //mt.RemoveAll(x => x.);
            int tmpCount = mt.Count;
            for (int j = 0; j < tmpCount; j++)
			{
                string dd = string.Empty;
                if (mt.Count == 0)
                {
                    j += tmpCount;
                }
                else
                {
                    if (j < mt.Count)
                    {
                        dd = mt[j].DuongDan;
                    }
                    else
                    {

                    }
                }
                
                
			    var lstPageRole = pq.layDSRoleTheoTenTrang(dd.ToLower());
                List<int> lstTemp = lstUserRole.Intersect(lstPageRole).ToList();
                if (lstPageRole.Count > 0)
	            {
		            if (lstTemp.Count == 0 && !lstUserRole.Contains(1))
	                {
                        mt.RemoveAll(x => x.DuongDan == dd);
                        j--;
	                }
                    else
                    {
                        //j--;
                    }
	            }
			}
            return mt;
        }
        public List<MENU_TRAI> LayDSTatCaMenuCapHai(int _CapMot)
        {
            return DB.MENU_TRAIs.Where(m => m.CapHai > 0 && m.CapMot == _CapMot).OrderBy(m => m.CapHai).ToList();
        }
        public MENU_TRAI LayThongTinMenuItemTheoSTT(int _stt)
        {
            MENU_TRAI m = DB.MENU_TRAIs.SingleOrDefault(mt => mt.STT == _stt);
                return m;
        }

        public MENU_TRAI LayThongTinMenuItemTheoDuongDan(string _DuongDan)
        {
            MENU_TRAI m = DB.MENU_TRAIs.SingleOrDefault(mt => mt.DuongDan == _DuongDan);
            return m;
        }
        public MENU_TRAI LayThongTinMenuItemTheoCapMenu(int? _CapMot, int? _CapHai)
        {
            MENU_TRAI m = DB.MENU_TRAIs.SingleOrDefault(mt => mt.CapMot == _CapMot && mt.CapHai == _CapHai);
            return m;
        }
        public bool ktMenuItemTonTai(int? _CapMot, int? _CapHai)
        {

            if (_CapHai == 0 && _CapMot > 0)
            {
                List<MENU_TRAI> m = DB.MENU_TRAIs.Where(a => a.CapMot == _CapMot).ToList();
                if (m.Count > 0)
                    return false;
            }
            else if (_CapHai > 0 && _CapMot > 0)
            {
                List<MENU_TRAI> m = DB.MENU_TRAIs.Where(a => a.CapMot == _CapMot && a.CapHai == _CapHai).ToList();
                if (m.Count > 0)
                    return false;
            }
            return true;

        }
        public void SwapMenuItem(int _CapMot, int _CapHai)
        { 
            
        }
        public string[] LayMenuItemCuoi() 
        {
            string[] strMinMax = new string[2];
            string max1 = DB.MENU_TRAIs.AsEnumerable()
                        .Max(row => row.CapMot)
                        .ToString();
            strMinMax[0] = max1;
            string max2 = DB.MENU_TRAIs.AsEnumerable()
                        .Max(row => row.CapHai)
                        .ToString();
            strMinMax[1] = max2;

            return strMinMax;
        }
        public void SuaMenuItem(MENU_TRAI _m)
        {
            MENU_TRAI menuItem = DB.MENU_TRAIs.SingleOrDefault(a => a.STT == _m.STT);
            if (_m.CapHai == 0)
            {
                menuItem.CapMot = _m.CapMot;
                menuItem.CapHai = 0;
            }
            else
            {
                menuItem.CapMot = _m.CapMot;// ?? int.MinValue;    
                menuItem.CapHai = _m.CapHai;// ?? int.MinValue;
            }
            
            menuItem.DuongDan = _m.DuongDan;
            menuItem.TenTrang = _m.TenTrang;
            menuItem.HienThi = _m.HienThi;
            DB.SubmitChanges();
        }
        public void XoaMenuItem(int? _CapMot, int? _CapHai)
        {
            if (_CapHai == 0 && _CapMot > 0)
            {
                List<MENU_TRAI> menuItem = DB.MENU_TRAIs.Where(a => a.CapMot == _CapMot).ToList();
                DB.MENU_TRAIs.DeleteAllOnSubmit(menuItem);
            }
            else
            {
                MENU_TRAI menuItem = DB.MENU_TRAIs.SingleOrDefault(a => a.CapMot == _CapMot && a.CapHai == _CapHai);
                DB.MENU_TRAIs.DeleteOnSubmit(menuItem);
            }
            DB.SubmitChanges();
        }
        public void XoaMenuItemDaChon(int _stt)
        {
            MENU_TRAI menuItem = DB.MENU_TRAIs.SingleOrDefault(a => a.STT == _stt);
            DB.MENU_TRAIs.DeleteOnSubmit(menuItem);
            DB.SubmitChanges();
        }
        public void XoaMenuItemCapMot(int _stt)
        {
            MENU_TRAI m = DB.MENU_TRAIs.SingleOrDefault(s => s.STT == _stt);
            List<MENU_TRAI> menuItem = DB.MENU_TRAIs.Where(a => a.CapMot == m.CapMot).ToList();
            DB.MENU_TRAIs.DeleteAllOnSubmit(menuItem);
            DB.SubmitChanges();
        }
        public string KiemTraTonTai(string _DuongDan, string _TenTrang)
        {
            if (DB.MENU_TRAIs.Any(a => a.DuongDan == _DuongDan))
                return "Đường dẫn ";
            if (DB.MENU_TRAIs.Any(b => b.TenTrang == _TenTrang))
                return "Tên trang ";
            else
                return null;

        }
        public void ThemMenuItem(MENU_TRAI _m)
        {
            DB.MENU_TRAIs.InsertOnSubmit(_m);
            DB.SubmitChanges();
        }
    }
}
